package com.xiaomaoguai.springmvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.xiaomaoguai.springmvc.model.AccountModel;

/**
 * @Description :
 * @FileName: DataBindController.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年5月18日 下午12:34:58
 * @Version:V1.00
 */
@Controller
@RequestMapping("/bind")
@SessionAttributes(value = "sessionaccountmodel")
public class DataBindController {

	@RequestMapping(value="/modelautobind", method = {RequestMethod.GET})
	public String modelAutoBind(HttpServletRequest request, Model model){
		model.addAttribute("accountmodel", new AccountModel());
		return "modelautobind";
	}
	
	
	@RequestMapping(value="/modelautobind", method = {RequestMethod.POST})
	public String modelAutoBind(HttpServletRequest request, @ModelAttribute("accountmodel") AccountModel accountModel){
		
		return "modelautobindresult";
	}
	
	//@CookieValue Test
	@RequestMapping(value="/cookiebind", method = {RequestMethod.GET})
    public String cookieBind(HttpServletRequest request, Model model, @CookieValue(value="JSESSIONID", defaultValue="") String jsessionId){
		
		model.addAttribute("jsessionId", jsessionId);
        return "cookiebindresult";
    }
	
	//@RequestHeader Test
	@RequestMapping(value="/requestheaderbind", method = {RequestMethod.GET})
    public String requestHeaderBind(HttpServletRequest request, Model model, @RequestHeader(value="User-Agent", defaultValue="") String userAgent){
		
		model.addAttribute("userAgent", userAgent);
        return "requestheaderbindresult";
    }
	
	//@SessionAttributes Test
	@ModelAttribute("sessionaccountmodel")
	public AccountModel initAccountModel(){
		
		return new AccountModel();
	}
	
	@RequestMapping(value="/usernamebind", method = {RequestMethod.GET})
	public String userNameBind( Model model, AccountModel accountModel){
		
		model.addAttribute("sessionaccountmodel", new AccountModel());
		return "usernamebind";
	}
	
	@RequestMapping(value="/usernamebind", method = {RequestMethod.POST})
	public String userNameBindPost( @ModelAttribute("sessionaccountmodel") AccountModel accountModel){
		
		//重定向到密码绑定测试
		return "redirect:passwordbind";
	}
	
	@RequestMapping(value="/passwordbind", method = {RequestMethod.GET})
	public String passwordBind(@ModelAttribute("sessionaccountmodel") AccountModel accountModel){
		
		return "passwordbind";
	}
	
	@RequestMapping(value="/passwordbind", method = {RequestMethod.POST})
	public String passwordBindPost(@ModelAttribute("sessionaccountmodel") AccountModel accountModel, SessionStatus status){
		
		//销毁@SessionAttributes中value指定名称的数据
		status.setComplete();
		//显示绑定结果
		return "sessionmodelbindresult";
	}
	
	//@RequestBody Test
	@RequestMapping(value="/requestbodybind", method = {RequestMethod.GET})
    public String requestBodyBind(Model model){
		
		model.addAttribute("accountmodel", new AccountModel());
        return "requestbodybind";
    }
	
	@RequestMapping(value="/requestbodybind", method = {RequestMethod.POST})
    public @ResponseBody AccountModel requestBodyBind(@RequestBody AccountModel accountModel){
				
		return accountModel;
    }
}
